package com.chen.pspms.service.impl;

import com.chen.pspms.entity.CompanyInfo;
import com.chen.pspms.entity.Holder;
import com.chen.pspms.mapper.CompanyMapper;
import com.chen.pspms.mapper.HolderMapper;
import com.chen.pspms.service.CompanyService;
import com.chen.pspms.utils.ApplicationException;
import com.chen.pspms.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    HolderMapper holderMapper;

    @Override
    public Integer save(CompanyInfo company) throws ApplicationException {
        int row=0;
        try {
            row = companyMapper.save(company);
        } catch (Exception e) {
            throw new ApplicationException("新增企业信息失败", e);
        }
        return row;
    }

    @Override
    public Integer delete(Integer id) throws ApplicationException {
        int row=0;
        try {
            row = companyMapper.delete(id);
        } catch (Exception e) {
            throw new ApplicationException("删除企业信息失败", e);
        }
        return row;
    }

    @Override
    @RedisCache(key = "#id")
    public CompanyInfo findyById(Integer id) throws ApplicationException {
        CompanyInfo company;
        try {
            company = companyMapper.findyById(id);
            Holder holder = holderMapper.findyById(company.getHolder().getId());
            company.setHolder(holder);
        } catch (Exception e) {
            throw new ApplicationException("查询企业信息失败", e);
        }
        return company;
    }

    @Override
    @RedisCache(key = "#name")
    public CompanyInfo findyByName(String name) throws ApplicationException {
        CompanyInfo company;
        try {
            company = companyMapper.findyByName(name);
            if(company!=null){
                Holder holder = holderMapper.findyById(company.getHolder().getId());
                company.setHolder(holder);
            }
        } catch (Exception e) {
            throw new ApplicationException("通过名称查询企业信息失败", e);
        }
        return company;
    }

    @Override
    public Integer update(CompanyInfo company) throws ApplicationException {
        int row=0;
        try {
            row = companyMapper.update(company);
        } catch (Exception e) {
            throw new ApplicationException("更新企业信息失败", e);
        }
        return row;
    }

    @Override
    public List<CompanyInfo> findAll() throws ApplicationException {
        List<CompanyInfo> companylist;
        try {
            companylist = companyMapper.findyAll();
            for (CompanyInfo company:companylist) {
                Holder holder = holderMapper.findyById(company.getHolder().getId());
                company.setHolder(holder);
            }
        } catch (Exception e) {
            throw new ApplicationException("查询企业信息列表失败", e);
        }
        return companylist;
    }
}
